package com.ai.flight.ai.controller;

import java.time.LocalDate;

import com.ai.flight.ai.tools.FlightBookingAiTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: suren@517na.com
 * @CreateTime: 2025-06-01
 * @Description:
 */

@RestController
@RequestMapping("/ai")
public class AiController {

    @Autowired()
    private ChatClient qwenChatClient;

//    @Autowired
//    public FlightBookingAiTools flightBookingAiTools;

    @CrossOrigin
    @GetMapping(value = "/generateStreamAsString", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStreamAsString(@RequestParam(value = "message", defaultValue = "讲个笑话") String message,
                                               @RequestParam(value = "userId", defaultValue = "zizhuan") String userId) {
        Flux<String> content = qwenChatClient.prompt()
//                .tools(flightBookingAiTools)
                .system(promptSystemSpec -> promptSystemSpec.param("current_date", LocalDate.now().toString()))
                .advisors(p -> p.param(ChatMemory.CONVERSATION_ID, userId))
                .user(message)
                .stream()
                .content();
        //和前端约定[complete]结尾，关闭sse长连接
        return content.concatWith(Flux.just("[complete]"));
    }
}
